package cn.al2.util.demo;

import cn.al2.util.GenerateUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class NIOFilesWriteDemo {
    public static void main(String[] args) throws IOException {

        final int target = 1000; // 目标倍数值
        StringBuffer sb = new StringBuffer();

        Path filePath = Paths.get("NIOFilesWriteDemo.txt");


        long start = System.currentTimeMillis();
        Files.write(filePath, "".getBytes(StandardCharsets.UTF_8));

        int count = 0;
        String sum = "";
        for (String logStr : GenerateUtil.getInstance().staticProduct()) {

            count++;

            if (count % target == 0) {

//                System.out.println(count);

                Files.write(filePath, sb.toString().getBytes(StandardCharsets.UTF_8),
                        java.nio.file.StandardOpenOption.APPEND);
                sb.setLength(0);
            } else {
                sb.append(logStr);

            }

        }
        long end = System.currentTimeMillis();

        System.out.println("NiO 文件写入成功 耗时：" + (end - start));
    }
}
